package com.ruoyi.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.text.Collator;
import java.util.*;

/**
 *  拼音工具类
 *
 * @author kuaidou
 */
public class PinyinUtlis {


    /**
     * 获取汉字串拼音全拼，英文字符不变
     * @param chinese 汉字串
     * @return 汉语拼音
     */
    public static String getFullSpell(String chinese) {

        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);

                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString();
    }

    /**
     * 获取汉字串首字母拼音
     * @param chinese 汉字串
     * @return 汉语拼音
     */
    public static String converterToFirstSpell(String chinese) {
        String pinyinName = "";
        char[] nameChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < nameChar.length; i++) {
            String s = String.valueOf(nameChar[i]);
            if (s.matches("[\\u4e00-\\u9fa5]")) {
                try {
                    String[] mPinyinArray = PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat);
                    if(mPinyinArray!=null){
                        mPinyinArray[0] = mPinyinArray[0].substring(0, 1);
                        pinyinName += mPinyinArray[0];
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName += nameChar[i];
            }
        }
        return pinyinName.toUpperCase();
    }

    /**
     * 根据姓名拼音排序
     * @param list 要进行排序的列表
     * @return
     */
    public static Map<String, Object> px (List<String> list){
        Comparator com = Collator.getInstance(Locale.CHINA);
        Collections.sort(list, com);
        Map<String, Object> map = new TreeMap<>();
        for (int i = 1; i<=26; i++){
            String word = String.valueOf((char) (96 + i)).toUpperCase();
            //循环找出首字母一样的数据
            List<String> letter = new ArrayList<>();
            for (String str: list){
                String zm = getFullSpell(str).substring(0, 1);
                if (word.equals(zm)){
                    letter.add(str);
                }
            }
            map.put(word, letter);
        }
        return map;
    }

}
